package com.probiz.estoresf.customer.web.action;


import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.helper.AttributeUtil;
import com.probiz.estore.common.model.attribute.AttributeValue;
import com.probiz.estore.common.model.customer.Customer;
import com.probiz.estore.core.controller.GenericCustomerFrontController;
import com.probiz.estore.core.exception.ApplicationException;
import com.probiz.estore.customer.model.CustomerFullyModel;
import com.probiz.estore.customer.service.CustomerManager;
import com.probiz.estore.webapp.util.RequestContext;

public class CustomerFrontController extends GenericCustomerFrontController<Customer> {
    private CustomerManager customerManager = null;
    
    public void setCustomerManager(CustomerManager customerManager) {
        this.customerManager = customerManager;
    }

	@Override
	protected void initController() throws Exception {
		super.mgr = customerManager;
	}
	@Override
	protected String getEntityName(Customer entity) {
		return null;
	}

	@Override
	protected Map<Integer, Map<String, Object>> getMultiSaveModel(
			HttpServletRequest request) {
		return null;
	}
    
	@Override
    public ModelAndView defaultAction(HttpServletRequest request, HttpServletResponse response) {
		Customer customer = customerManager.getByAppUserId(RequestContext.getCurrentUserId());
		request.setAttribute("customer", customer);
		ModelAndView mv = new ModelAndView("customer/csCustomerForm");
		mv.addObject("customer", customer);
		return mv;
    }
	
	@Override
	protected void preSave(HttpServletRequest request, Customer entity, BindException errors) {
		Customer customer = entity;
		//检查修改后的邮件是否已经存在
		if (customerManager.getIsEmailUseByOthers(customer.getEmail(), customer.getAppuserId())) {
			errors.rejectValue("email", "appUser.email.isExist", "the email has use by others");
		}
	}
	
	@Override
    public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Customer customer = customerManager.getByAppUserId(RequestContext.getCurrentUserId());
		BindException errors = null;
		try {
			ServletRequestDataBinder binder = bindAndValidate(request, customer);
			errors = new BindException(binder.getBindingResult());
			preSave(request, customer, errors);
			if (!errors.hasErrors()) {
				CustomerFullyModel model = new CustomerFullyModel();
				model.setCustomer(customer);
				List<AttributeValue> list = AttributeUtil.getInstance().getAttributeFromRequest(request);
				model.setAttributeValues(list);
				customerManager.saveFully(model);
				
				String key = "customer.updated";
		        String message = getMessage(key, new Object[] {customer.getUsername()});  
		        saveMessage(request, message);  
			}
		} catch (ApplicationException e) {
			handleApplicationException(errors, e);
		}

		request.setAttribute("customer", customer);
		
		ModelAndView mav = new ModelAndView("customer/csCustomerForm");
		if (errors.hasErrors()) {
			mav = getModelAndView("customer/csCustomerForm", errors.getModel());
		}
		return mav;
	}

	

	
	
}
